#!/bin/bash

# ABACUS势能面扫描脚本 - 调试版本

# 设置
ABACUS_PATH="/home/zxg/software/abacus3.9.014/abacus-develop/build/abacus"
CORE=2  # 先用2个核心测试
PSEUDO_DIR=" home/zxg/software/abacus3.9.014/abacus-develop/potential"

# 检查环境
echo "=== 环境检查 ==="
if [ ! -f "$ABACUS_PATH" ]; then
    echo "错误: ABACUS不存在: $ABACUS_PATH"
    exit 1
fi

if [ ! -d "$PSEUDO_DIR" ]; then
    echo "错误: 赝势目录不存在: $PSEUDO_DIR"
    exit 1
fi

# 创建工作目录
WORK_DIR="PES_scan_3D_debug"
mkdir -p $WORK_DIR
cd $WORK_DIR

# 设置输入文件
echo "=== 设置输入文件 ==="
cat > INPUT << 'EOF'
#INPUT_PARAMETERS
pseudo_dir      ./potential
orbital_dir     ./potential

#Parameters (1.General)
calculation     scf

#Parameters (2.Iteration)
ecutwfc         80
scf_nmax        50
dr2             1e-5

#Parameters (3.Basis)
basis_type      lcao
symmetry        0
kspacing        0.1 0.1 0.2

#Parameters (4.Smearing)
smearing_method gauss
smearing_sigma  0.01

#Parameters (5.Mixing)
mixing_type     pulay
mixing_beta     0.7

cal_stress      1
cal_force       1
EOF

# 复制赝势文件
mkdir -p potential
cp $PSEUDO_DIR/*.upf potential/
echo "赝势文件: $(ls potential/*.upf)"

# 测试一个点
echo "=== 测试单个计算 ==="
mkdir -p test_point
cd test_point

# 生成测试STRU文件
cat > STRU << 'EOF'
ATOMIC_SPECIES
C   12.011  C_ONCV_PBE-1.0.upf
Al   26.982  Al_ONCV_PBE-1.0.upf

NUMERICAL_ORBITAL
C_gga_7au_100Ry_2s2p1d.orb
Al_gga_7au_100Ry_4s4p1d.orb

LATTICE_CONSTANT
1.889726

LATTICE_VECTORS
12.68000000000  0.000000000000  0.000000000000
-6.34000000000  10.98120211998  0.000000000000
0.000000000000  0.000000000000  20.00000000000

ATOMIC_POSITIONS
Direct

C 
0.000
48
0.129830000000  0.064920000000  0.000000000000  1  1  1
0.435090000000  0.064910000000  0.000000000000  1  1  1
0.435090000000  0.370170000000  0.000000000000  1  1  1
0.370170000000  0.435090000000  0.000000000000  1  1  1
0.064920000000  0.435090000000  0.000000000000  1  1  1
0.064920000000  0.129830000000  0.000000000000  1  1  1
0.258770000000  0.129390000000  0.000000000000  1  1  1
0.370620000000  0.129380000000  0.000000000000  1  1  1
0.369800000000  0.240410000000  0.000000000000  1  1  1
0.241240000000  0.370620000000  0.000000000000  1  1  1
0.129390000000  0.370620000000  0.000000000000  1  1  1
0.129380000000  0.258770000000  0.000000000000  1  1  1
0.629830000000  0.064920000000  0.000000000000  1  1  1
0.935090000000  0.064910000000  0.000000000000  1  1  1
0.935090000000  0.370170000000  0.000000000000  1  1  1
0.870170000000  0.435090000000  0.000000000000  1  1  1
0.564920000000  0.435090000000  0.000000000000  1  1  1
0.564920000000  0.129830000000  0.000000000000  1  1  1
0.758770000000  0.129390000000  0.000000000000  1  1  1
0.870620000000  0.129380000000  0.000000000000  1  1  1
0.869800000000  0.240410000000  0.000000000000  1  1  1
0.741240000000  0.370620000000  0.000000000000  1  1  1
0.629390000000  0.370620000000  0.000000000000  1  1  1
0.629380000000  0.258770000000  0.000000000000  1  1  1
0.129830000000  0.564920000000  0.000000000000  1  1  1
0.435090000000  0.564920000000  0.000000000000  1  1  1
0.435090000000  0.870170000000  0.000000000000  1  1  1
0.370170000000  0.935090000000  0.000000000000  1  1  1
0.064920000000  0.935090000000  0.000000000000  1  1  1
0.064920000000  0.629830000000  0.000000000000  1  1  1
0.258770000000  0.629390000000  0.000000000000  1  1  1
0.370620000000  0.629380000000  0.000000000000  1  1  1
0.369800000000  0.740410000000  0.000000000000  1  1  1
0.241240000000  0.870620000000  0.000000000000  1  1  1
0.129390000000  0.870620000000  0.000000000000  1  1  1
0.129380000000  0.758770000000  0.000000000000  1  1  1
0.629830000000  0.564920000000  0.000000000000  1  1  1
0.935090000000  0.564920000000  0.000000000000  1  1  1
0.935090000000  0.870170000000  0.000000000000  1  1  1
0.870170000000  0.935090000000  0.000000000000  1  1  1
0.564920000000  0.935090000000  0.000000000000  1  1  1
0.564920000000  0.629830000000  0.000000000000  1  1  1
0.758770000000  0.629390000000  0.000000000000  1  1  1
0.870620000000  0.629380000000  0.000000000000  1  1  1
0.869800000000  0.740410000000  0.000000000000  1  1  1
0.741240000000  0.870620000000  0.000000000000  1  1  1
0.629390000000  0.870620000000  0.000000000000  1  1  1
0.629380000000  0.758770000000  0.000000000000  1  1  1

Al
0.000
1
0.25  0.25  0.09  1  1  1
EOF

cp ../INPUT ./
cp -r ../potential ./

echo "=== 开始测试计算 ==="
export OMP_NUM_THREADS=1
mpirun -n $CORE $ABACUS_PATH > out.log 2> err.log

echo "=== 计算结果 ==="
if [ -f "OUT.ABACUS/running_scf.log" ]; then
    if grep -q "FINAL_ETOT_IS" OUT.ABACUS/running_scf.log; then
        energy=$(grep "FINAL_ETOT_IS" OUT.ABACUS/running_scf.log | tail -1 | awk '{print $2}')
        echo "✓ 测试成功! 能量: $energy Ry"
        echo "=== 开始正式扫描 ==="
        # 这里可以继续正式扫描
    else
        echo "✗ 测试失败 - 计算未收敛"
        echo "错误日志:"
        cat err.log
        echo "输出日志最后20行:"
        tail -20 out.log
    fi
else
    echo "✗ 测试失败 - 无输出文件"
    echo "错误日志:"
    cat err.log
fi